﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Atlandiz.Data;


namespace Atlandiz.EnvanterVeZimmetYonetimi
{
    public partial class FormDepoTanitimKarti : DevExpress.XtraEditors.XtraForm
    {
        //MsSqlAccessLayer SQL = new MsSqlAccessLayer(".\\SQLExpress", "TeknoEnva", true);
        public int KayID;

        public FormDepoTanitimKarti()
        {
            InitializeComponent();
        }

        private void DepoTanitimKarti_Load(object sender, EventArgs e)
        {
            switch (KayID) { 
                case 0:
                    FormReset();

                    break;
                default:
                    FormReset();
                    KayitGetir();
                    break;            
            }

        }

        public void KayitGetir() {
            DataTable dt = Army.SQL().GetDataTable("Select * from Depolar where id=" + KayID);
            if (dt.Rows.Count > 0) {

                _txtDepoAdiString.Text = dt.Rows[0][5].ToString();
                _txtAciklamaString.Text = dt.Rows[0][6].ToString();
                _txtAdresString.Text = dt.Rows[0][7].ToString();
                _txtTelefonString.Text = dt.Rows[0][8].ToString();
                _txtFaxString.Text = dt.Rows[0][9].ToString();
                _txtEPostaString.Text = dt.Rows[0][10].ToString();
                _txtYetkiliIdInt.Text = dt.Rows[0][11].ToString();
                _txtYetkiliIdString.Text = Army.SQL().GetDataCell("Select Ad + ' ' + Soyad from Personel where pID=" + _txtYetkiliIdInt.Text);

                _txtDepoAdiString.Focus();
            
            }
        }

        public void FormReset() {
            _txtDepoAdiString.Text = "";
            _txtAciklamaString.Text = "";
            _txtAdresString.Text = "";
            _txtTelefonString.Text = "";
            _txtFaxString.Text = "";
            _txtEPostaString.Text = "";
            _txtYetkiliIdString.Text = "";
            _txtYetkiliIdInt.Text = "";

            _txtDepoAdiString.Focus();
        }


        private void _btnSec_Click(object sender, EventArgs e)
        {
            ListePersonel kl = new ListePersonel();
            kl.ForSelection = true;
            kl.ShowDialog();

            if (Army.EventConfirm == true)
            {
                _txtYetkiliIdInt.Text = Army.SecilenID.ToString();
                _txtYetkiliIdString.Text = Army.SQL().GetDataCell("Select Ad + ' ' + Soyad from Personel where pID=" + _txtYetkiliIdInt.Text);
            }
        }

        private void _btnKapat_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void _btnKaydet_Click(object sender, EventArgs e)
        {

            if (_txtDepoAdiString.Text.ToString().Length < 1)
            {
                MessageBox.Show("Depo Adı Girmelisiniz!", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                _txtDepoAdiString.Focus();
                return;
            }

            if (_txtYetkiliIdInt.Text.ToString().Length < 1)
            {
                _txtYetkiliIdInt.Text = "0";
            }

            string Sorgu="";

            switch (KayID) { 
                case 0:
                    Sorgu = "INSERT INTO Depolar (CreateDate,CreateUser,UpdateDate,UpdateUser,DepoAdi,Aciklama,Adres,Telefon,Fax,Email,YetkiliId) \n"+ 
                        "VALUES(\n" +
                        "Convert(datetime,'" + DateTime.Now + "',104),\n" + //CreateDate
                        Army.ActiveUserId + ",\n" + //CreateUser
                        "Convert(datetime,'" + DateTime.Now + "',104),\n" + //UpdateDate
                        Army.ActiveUserId + ",\n" + //UpdateUser
                        "'" + _txtDepoAdiString.Text.ToString().Replace("'", "''") + "',\n" + //DepoAdi
                        "'" + _txtAciklamaString.Text.ToString().Replace("'", "''") + "',\n" + //Aciklama
                        "'" + _txtAdresString.Text.ToString().Replace("'", "''") + "',\n" + //Adres
                        "'" + _txtTelefonString.Text.ToString().Replace("'", "''") + "',\n" + //Telefon
                        "'" + _txtFaxString.Text.ToString().Replace("'", "''") + "',\n" + //Fax
                        "'" + _txtEPostaString.Text.ToString().Replace("'", "''") + "',\n" + //Email
                        _txtYetkiliIdInt.Text + "\n" + //YetkiliId
                        ")";

                    Army.SQL().cmd(Sorgu);
                    FormReset();

                    break;
                default:
                    Sorgu = "UPDATE Depolar SET\n" +
                        "UpdateDate=Convert(datetime,'" + DateTime.Now + "',104),\n" + //UpdateDate
                        "UpdateUser=" + Army.ActiveUserId + ",\n" + //UpdateUser
                        "DepoAdi='" + _txtDepoAdiString.Text.ToString().Replace("'", "''") + "',\n" + //DepoAdi
                        "Aciklama='" + _txtAciklamaString.Text.ToString().Replace("'", "''") + "',\n" + //Aciklama
                        "Adres='" + _txtAdresString.Text.ToString().Replace("'", "''") + "',\n" + //Adres
                        "Telefon='" + _txtTelefonString.Text.ToString().Replace("'", "''") + "',\n" + //Telefon
                        "Fax='" + _txtFaxString.Text.ToString().Replace("'", "''") + "',\n" + //Fax
                        "Email='" + _txtEPostaString.Text.ToString().Replace("'", "''") + "',\n" + //Email
                        "YetkiliId=" + _txtYetkiliIdInt.Text + "\n" + //YetkiliId
                        "Where id=" + KayID;

                    Army.SQL().cmd(Sorgu);
                    this.Close();
                    break;
            }
        }

        private void _btnDepartmanListesi_Click(object sender, EventArgs e)
        {
            ListeDepolar listeDepolar = new ListeDepolar();
            listeDepolar.ShowDialog();
        }

      

    }
}